package com.neusoft.controller;

import java.util.List;

import com.neusoft.po.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;


import com.neusoft.po.VirtualWallet;
import com.neusoft.po.VirtualWalletTransaction;
import com.neusoft.service.VirtualWalletService;

@RestController
@RequestMapping("/VirtualWalletController")
@RefreshScope
public class VirtualWalletController {
	@Autowired
	VirtualWalletService virtualWalletService;

	//支持功能，给新用户创建一个钱包
	@PostMapping(value="/wallets/{userId}")
	public CommonResult<Integer> saveWallet(@PathVariable("userId") String userId) {

		int result = virtualWalletService.saveWallet(userId);

		return new CommonResult<>(200, "success", result);
	}

	//按照userId获得一个wallet
	@GetMapping("/getWallet/{userId}")
	public CommonResult<VirtualWallet> getWalletByUserId(@PathVariable("userId") String userId) {
		VirtualWallet result = virtualWalletService.getWalletByUserId(userId);
		return new CommonResult<>(200, "success", result);
	}
	
	//充值
	//传入walletId:数据库中userId未使用外键，在这里传入walletId进行一致性检验
	@PutMapping(value="/wallets/{walletId}/userId/{userId}/rechargeAmount/{rechargeAmount}")
	public CommonResult<Integer> recharge(@PathVariable("walletId") Integer walletId,
										  @PathVariable("userId") String userId,
										  @PathVariable("rechargeAmount") Double rechargeAmount) {
		//if addAmount>=0
		//if < 0
		int result = virtualWalletService.recharge(walletId, userId, rechargeAmount);
		return new CommonResult<>(200, "success", result);
	}

	//提现
	@PutMapping(value="/wallets/{walletId}/userId/{userId}/withdrawAmount/{withdrawAmount}")
	public CommonResult<Integer> withdraw(@PathVariable("walletId") Integer walletId,
										  @PathVariable("userId") String userId,
										  @PathVariable("withdrawAmount") Double withdrawAmount) {
		int result = virtualWalletService.withdraw(walletId, userId, withdrawAmount);
		return new CommonResult<>(200, "success", result);
	}

	//支付
	//可以换成传入对象，简化
	@PutMapping(value="/wallets/{walletId}/orderId/{orderId}/userId/{userId}/paymentAmount/{paymentAmount}")
	public CommonResult<Integer> payment(@PathVariable("orderId") Integer orderId,
										 @PathVariable("walletId") Integer walletId,
										 @PathVariable("userId") String userId,
										 @PathVariable("paymentAmount") Double paymentAmount) {
		int result = virtualWalletService.payment(orderId, walletId, userId, paymentAmount);
		return new CommonResult<>(200, "success", result);
	}
	

	
//	//查询余额
//	@RequestMapping(value="/wallets/userId/{userId}", method = RequestMethod.GET)
//	public Double getBalanceByUserId(@PathVariable("userId") String userId) {
//		return virtualWalletService.getBalanceByUserId(userId);
//	}
	

	//查询交易流水
	@GetMapping(value="/walletTransactions/walletId/{walletId}")
	public CommonResult<List<VirtualWalletTransaction>> getTransactionBytransactionId(@PathVariable("walletId") Integer walletId) {
		List<VirtualWalletTransaction> result = virtualWalletService.getTransaction(walletId);
		return new CommonResult<>(200, "success", result);
	}
	
	
}
